package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class AdvposController {
    @Autowired
    private AdvposService advposService;

    @Autowired
    private AdvimgService advimgService;

    /**
     * 分页查询
     *
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String advposdesc, String advposname, int page, int limit) {
        log.info("广告描述:{},广告名:{},当前页面:{},页面展示条数:{}", advposdesc, advposname, page, limit);
        //构造分页构造器
        Page<Advpos> pageInfo = new Page<>(page, limit);
        //条件构造器
        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(advposdesc != null, Advpos::getAdvposdesc, advposdesc)
                .like(advposname != null, Advpos::getAdvposname, advposname);
        //添加排序条件
        queryWrapper.orderByDesc(Advpos::getId);
        //执行分页查询
        advposService .page(pageInfo, queryWrapper);

        //处理Dish对象的分类信息
        Page<AdvposDto> advposDtoPage = new Page<>();

        BeanUtils.copyProperties(pageInfo, advposDtoPage, "records");

        List<Advpos> yddList = pageInfo.getRecords();

        List<AdvposDto> advposDtoList = yddList.stream().map(advpos -> {
            AdvposDto advposDto = new AdvposDto();
            BeanUtils.copyProperties(advpos, advposDto);

            LambdaQueryWrapper<Advimg> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Advimg::getAdvimgpos, advpos.getId());

            int count = advimgService.count(queryWrapper1);
            advposDto.setAdvposnum(count);

            return advposDto;
        }).collect(Collectors.toList());
        advposDtoPage.setRecords(advposDtoList);

        long total = advposDtoPage.getTotal();
        long size = advposDtoPage.getSize();
        long current = advposDtoPage.getCurrent();

        return ResultData.ok("", PageResult.init(total, size, current, advposDtoList));
    }

    /**
     * 根据id查询,数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData findById(Long id) {
        log.info("id:{}", id);
        Advpos advpos = advposService.getById(id);
        return ResultData.ok("", advpos);
    }

    /**
     * 修改数据
     *
     * @param advpos
     * @return
     */
    @PostMapping("/update")
    public ResultData Update(@RequestBody Advpos advpos) {
        log.info("广告位:{}", advpos);
        String id = advpos.getId();
        //先根据id删除,再重新添加
        advposService.removeById(id);
        advposService.save(advpos);
        return ResultData.ok("修改成功", null);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData Detel(String id) {
        List<Advimg> advimgs = advimgService.getByPos(id);
        if(advimgs.size()!=0){
            return ResultData.error("该数据已关联其他表,无法删除 ");
        }
        advposService.removeById(id);
        return ResultData.ok("删除成功", null);
    }


    /**
     * 添加
     *
     * @param advpos
     * @return
     */
    @PostMapping("/add")
    public ResultData Add(@RequestBody Advpos advpos) {
        advposService.save(advpos);
        return ResultData.ok("添加成功", null);
    }

    /**
     * 查询所有广告位
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData All() {
        List<Advpos> list = advposService.list();
        return ResultData.ok("", list);
    }
}
